package system.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import common.config.http.HttpStatusEnum;
import common.mapper.CommonSysLogMapper;
import common.model.po.SysLog;
import common.model.request.base.PaginationRequest;
import org.springframework.stereotype.Service;
import system.model.request.SearchMyselfSystemLogRequest;
import system.model.request.SearchSystemLogRequest;
import system.service.SysLogService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/**
 * 服务实现类
 *
 * @author 米泽鹏
 * @since 2022-03-22
 */
@Service
public class SysLogServiceImpl extends ServiceImpl<CommonSysLogMapper, SysLog> implements SysLogService {

	private static final String SYSTEM_LOG_PAGE_QUERY_URL = "/system/sys-log/readOnly/pageSystemLog";
	private static final String MYSELF_SYSTEM_LOG_PAGE_QUERY_URL = "/sys-myself/pageSystemLog";
	@Resource
	private CommonSysLogMapper mapper;

	@Override
	public Page<SysLog> pageSystemLog(PaginationRequest<SearchSystemLogRequest, SysLog> paginationRequest) {
		LambdaQueryWrapper<SysLog> queryWrapper = paginationRequest.buildQueryWrapper().lambda();
		SearchSystemLogRequest request = paginationRequest.getParams();
		// 执行耗时不小于
		Long minExecutionTime = request.getMinExecutionTime();
		queryWrapper.ge(Objects.nonNull(minExecutionTime), SysLog::getExecutionTime, minExecutionTime);
		// 创建时间
		Date createTimeFrom = request.getCreateTimeFrom();
		Date createTimeTo = request.getCreateTimeTo();
		queryWrapper.between(Objects.nonNull(createTimeFrom) && Objects.nonNull(createTimeTo), SysLog::getCreateTime, createTimeFrom, createTimeTo);
		queryWrapper.ge(Objects.nonNull(createTimeFrom) && Objects.isNull(createTimeTo), SysLog::getCreateTime, createTimeFrom);
		queryWrapper.le(Objects.isNull(createTimeFrom) && Objects.nonNull(createTimeTo), SysLog::getCreateTime, createTimeTo);
		// 仅错误
		Boolean errorOnly = request.getErrorOnly();
		queryWrapper.ne(Objects.equals(errorOnly, true), SysLog::getStatusCode, HttpStatusEnum.SUCCESS.getCode());
		// 过滤系统日志的查询记录
		Boolean recordsOfSystemLogQueryNotIncluded = request.getRecordsOfSystemLogQueryNotIncluded();
		queryWrapper.ne(Objects.equals(recordsOfSystemLogQueryNotIncluded, true), SysLog::getRequestUrl, SYSTEM_LOG_PAGE_QUERY_URL);
		// 排序
		queryWrapper.orderByDesc(CollUtil.isEmpty(request.getOrderby()), SysLog::getCreateTime).orderByDesc(SysLog::getId);
		return page(paginationRequest.buildPage(), queryWrapper);
	}

	@Override
	public Page<SysLog> pageMyselfSystemLog(PaginationRequest<SearchMyselfSystemLogRequest, SysLog> paginationRequest) {
		LambdaQueryWrapper<SysLog> queryWrapper = paginationRequest.buildQueryWrapper().lambda();
		// 过滤系统日志的查询记录
		queryWrapper.ne(SysLog::getRequestUrl, MYSELF_SYSTEM_LOG_PAGE_QUERY_URL);
		// 排序
		queryWrapper.orderByDesc(SysLog::getCreateTime).orderByDesc(SysLog::getId);
		return page(paginationRequest.buildPage(), queryWrapper);
	}

}
